package edu.bw.dao.impl;

import edu.bw.dao.ProductDao;
import edu.bw.po.Product;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.sql.SQLException;
import java.util.List;
@Repository

public class ProductDaoImpl implements ProductDao {
    @Autowired
//    @Qualifier("queryRunner")
    private QueryRunner qr;
    @Override
    public void updateProduct(Product product) {
        try {
            qr.update("UPDATE spring.product p " +
                    "SET p.pname=?,p.price=?,p.category=?" +
                    "WHERE p.pid=?",
                    product.getPname(),
                    product.getPrice(),
                    product.getCategory(),
                    product.getPid());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<Product> findAllProducts() {
        try {
            return qr.query("SELECT * FROM spring.product",new BeanListHandler<Product>(Product.class));
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
